package com.data.center.service.neo4j;
import com.data.center.entity.neo4j.neoOrganization;
import com.data.center.mapper.neo4j.neoOrganizationRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.data.center.entity.neo4j.Approver;
import com.data.center.mapper.neo4j.ApproverRepository;
@Service
public class neoOrganizationService {
    @Autowired
    private neoOrganizationRepository organizationRepository;
    @Autowired
    private ApproverRepository approverRepository;

    public neoOrganization saveOrganization(neoOrganization organization) {
        Approver approver = approverRepository.findByIdentifier(organization.getApprover())
                .orElseGet(() -> {
                    // 如果审核人不存在，自动创建审核人实体
                    Approver newApprover = new Approver();
                    newApprover.setApproverId(organization.getApprover()); // 使用组织中的 approver 字段作为审核人 ID
                    newApprover.setName("Unknown Approver"); // 默认名称
                    newApprover.setPosition("Approver"); // 默认职位
                    return approverRepository.save(newApprover);
                });

        // 设置审核人关系
        organization.setApproverEntity(approver);


        return organizationRepository.save(organization);
    }

    public neoOrganization getOrganizationById(String organizationId) {
        return organizationRepository.findByOrganizationId(organizationId);
    }
}
